capture log close
log using $log/AED_dataprep_05_outcome_reg_monthly, text replace
   
clear
set mem 6000m
set matsize 800
set more off

set trace off
   
/* ------------------------------------------------------------------------------------ */
/* --- Generating monthly regional outcome variables based on the weekly variables ---- */
/* ------------------------------------------------------------------------------------ */


local iebversion v901  

forvalue year = 1999/2010 {
	
	use $data/outcome_reg_weekly_y`year'_`iebversion', replace
	

	foreach quelle in wo_kreis wo_aa ao_aa traeger {

		foreach month in 01 02 03 04 05 06 07 08 09 10 11 12 {
		
		
		di "### `year' `month' `quelle'"
		di c(current_time)
		
		
			/*Generating monthly indicator*/
			gen iza_`quelle'_`year'`month'="9999"
	
			/*First, all four weeks within one month have same info*/
			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0
			drop help

			/*Second, three out of four weeks within one month have same info*/
			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999"
			drop help

			egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'=="9999"
			drop help

			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999"
			drop help

			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999"
			drop help


			/*Third, two out of four weeks within one month have same info and non-missing*/
			egen help_miss = rowmiss(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
	
			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'1~=""
			drop help
			
			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'3)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'1~=""
			drop help
			
			egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'1~=""
			drop help
			
			egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'2~=""
			drop help
			
			egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'2~=""
			drop help
			
			egen help = diff(iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'3 if help == 0 & iza_`quelle'_`year'`month'=="9999" & help_miss<=2 & iza_`quelle'_`year'`month'3~=""
			drop help
			
			
			/*Fourth, month gets out-of-labour force if two weeks have a missing, and the other two weeks have different info*/
			replace iza_`quelle'_`year'`month' = "" if help_miss == 2 & iza_`quelle'_`year'`month'=="9999"
			
			drop help_miss

			/*Fifth, now we have only months left with 4 different infos. We choose the one in the middle of the month to dominate*/
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if iza_`quelle'_`year'`month'2~="" & iza_`quelle'_`year'`month'=="9999"
			replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'3 if iza_`quelle'_`year'`month'3~="" & iza_`quelle'_`year'`month'=="9999"
			
			
			replace iza_`quelle'_`year'`month' = "" if iza_`quelle'_`year'`month'=="9999"
			
			/*weekly infos are removed*/
			drop iza_`quelle'_`year'`month'1
			drop iza_`quelle'_`year'`month'2
			drop iza_`quelle'_`year'`month'3
			drop iza_`quelle'_`year'`month'4
			
			
		}/*end:month*/
	}/*end:quelle*/


	compress
	save $data/outcome_reg_monthly_y`year'_`iebversion', replace
	erase $data/outcome_reg_weekly_y`year'_`iebversion'.dta
}/*end:year*/


log close			


